<!--
 * @Description: 热销商品占比 组件
 * @Author: zhihong deng
 * @Date: 2021-07-04 21:40:02
 * @LastEditors: zhihong deng
 * @LastEditTime: 2021-07-10 23:17:38
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="arr-left" @click="toLeft" :style="comStyle">◁</span>
    <span class="arr-right" @click="toRight" :style="comStyle">▷</span>
    <span class="cat-name" :style="comStyle">{{catName}}</span>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils.js'
export default {
  data () {
    return {
      chartInstance: '',
      allData: '',
      currentIndex: 0,//当前所展示出的一级分类数据
      titleFontSize: 0,
    }
  },
  computed: {
    ...mapState(['theme']),
    catName () {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.currentIndex].name;
      }
    },
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor,
      }
    },
  },
  watch: {
    theme () {
      console.log('主题切换了');
      this.chartInstance.dispose();//销毁当前的图表
      this.initChart();//重新以最新的主题名称初始化图表对象
      this.screenAdapter();//完成屏幕的适配
      this.updataChart();//更新图表的展示
    }
  },
  methods: {
    // 初始化图表
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref,this.theme);
      const initOption = {
        title: {
            text: '▎热销商品的占比',
            left: 20,
            top: 20,
        },
        legend: {
          top: '15%',
          icon: 'circle',
        },
        // 文字提示
        tooltip: {
          show: true,
          formatter: arg => {
            const thirdCategory = arg.data.children;
            // 计算出所有三级分类的数值总和
            let total = 0;
            thirdCategory.forEach(item => {
              total += item.value
            });
            let retStr = '';
            thirdCategory.forEach(item => {
              retStr += `
                ${item.name}:${parseInt(item.value / total*100) + '%'}
                <br/>
              `
            })
            return retStr
          },
        },
        series: [
          {
            type: 'pie',
            label: {
              show: false,
            },
            // 高亮状态下文字显示
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              }
            },
          }
        ],
      };
      this.chartInstance.setOption(initOption);
    },
    // 获取数据
    getData (ret) {
      // const {data: ret} = await this.$http.get('hotproduct');
      this.allData = ret;
      console.log('全部数据：',this.allData);
      this.updataChart();//获取完数据再更新图表
    },
    // 更新图表
    updataChart () {
      // 处理图表需要的数据
      const legendData = this.allData[this.currentIndex].children.map(item => {
        return item.name
      })
      const seriesData = this.allData[this.currentIndex].children.map(item => {
        return {
          name: item.name,
          value: item.value,
          children: item.children,
        }
      })
      const dataOption = {
        legend: {
          data: legendData,
        },
        series: [
          {
            data: seriesData,
          }
        ],
      }
      this.chartInstance.setOption(dataOption);
    },
    // 分辨率适配
    screenAdapter () {
      this.titleFontSize = this.$refs.hot_ref.offsetWidth / 100 * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          }
        },
        series: [
          {
            radius: this.titleFontSize * 4.5,
            center: ['50%','50%'],
          }
        ],
      }
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    toLeft () {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updataChart();
    },
    toRight () {
      this.currentIndex++;
       if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updataChart();
    },
  },
  created () {
    // 在组件创建完成之后 进行回调函数的注册
    this.$socket.registerCallBack('hotData', this.getData)
  },
  mounted () {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'hotData',
      chartName: 'hot',
      value: ''
    })
    window.addEventListener('resize',this.screenAdapter);
    this.screenAdapter();
  },
  destroyed () {
    window.removeEventListener('resize',this.screenAdapter);
    // 在组件销毁的时候进行回调函数的取消
    this.$socket.unRegisterCallBack('hotData')
  },
}
</script>

<style>
.arr-left{
  position: absolute;
  left: 10%;
  top: 50%;
  /* transform: translateY(-50%); */
  cursor: pointer;
  color: white;
}
.arr-right{
  position: absolute;
  right: 10%;
  top: 50%;
  /* transform: translateY(-50%); */
  cursor: pointer;
  color: white;
}
.cat-name{
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: white;
}
</style>